package kr.nexmore.slmsample2.view.boardsample.thirdpart;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import kr.nexmore.slmsample2.dao.MyBoardDao;
import kr.nexmore.slmsample2.uri.Topic_Uri;
import kr.nexmore.slmsample2.vo.BoardVo;

import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.UIEventTopic;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class UpdatePart {
	@Inject
	private IEventBroker broker;

	private Text title_txt;
	private Text name_txt;
	private Text command_txt;

	private BoardVo boardVo;

	public UpdatePart() {

	}

	/**
	 * Create contents of the view part.
	 */
	@PostConstruct
	public void createControls(Composite parent) {
		parent.setLayout(null);
		title_txt = new Text(parent, SWT.BORDER);
		title_txt.setBounds(105, 84, 312, 21);

		Label lblNewLabel = new Label(parent, SWT.NONE);
		lblNewLabel.setBounds(31, 87, 56, 15);
		lblNewLabel.setText("제목");

		Label label = new Label(parent, SWT.NONE);
		label.setText("내용");
		label.setBounds(31, 126, 56, 15);

		Label label_1 = new Label(parent, SWT.NONE);
		label_1.setText("이름");
		label_1.setBounds(31, 44, 56, 15);

		name_txt = new Text(parent, SWT.BORDER);
		name_txt.setBounds(105, 41, 73, 21);

		Button btnNewButton = new Button(parent, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				boardVo.setName(name_txt.getText());
				boardVo.setTitle(title_txt.getText());
				boardVo.setCommand(command_txt.getText());

				MyBoardDao boardDao = new MyBoardDao();
				boardDao.update(boardVo);
				broker.post(Topic_Uri.BOARD_PART_REFRESH, null);
			}
		});
		btnNewButton.setBounds(188, 250, 76, 25);
		btnNewButton.setText("수정");

		command_txt = new Text(parent, SWT.BORDER);
		command_txt.setBounds(29, 147, 388, 97);
	}

	@PreDestroy
	public void dispose() {
	}

	@Focus
	public void setFocus() {
		// TODO Set the focus to control
	}

	@Inject
	@Optional
	public void receiveEvent(@UIEventTopic(Topic_Uri.THIRD_PART_UPDATE_SENDVO) BoardVo vo) {
		this.boardVo = vo;
		title_txt.setText(vo.getTitle());
		name_txt.setText(vo.getName());
		command_txt.setText(vo.getCommand());

	}
}
